<template>
  <div class="canvas-board">
    <div class="canvas-container" ref="canvasContainer">
      <div :id="canvasId"></div>
    </div>
  </div>
</template>

<script>
import { KonvaJs } from '../utils/konva';

export default {
  name: 'CanvasBoard',
  props: {
    canvasId: {
      type: String,
      default: 'canvas'
    }
  },
  data() {
    return {
      konva: null
    };
  },
  mounted() {
    this.initCanvas();
    window.addEventListener('resize', this.handleResize);
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.handleResize);
    if (this.konva) {
      this.konva.destroy();
    }
  },
  methods: {
    initCanvas() {
      this.konva = new KonvaJs(this.canvasId);
      this.konva.drawBoard();
    },
    handleResize() {
      if (this.konva) {
        this.konva.onresize();
      }
    },
    // 添加矩形
    addRect() {
      if (this.konva) {
        this.konva.addRect();
      }
    },
    // 添加文本
    addText(text) {
      if (this.konva) {
        this.konva.addText(text);
      }
    },
    // 添加图片
    addImage(imageData) {
      if (this.konva) {
        this.konva.addImage(imageData);
      }
    },
    // 清除画布
    clearCanvas() {
      if (this.konva) {
        this.konva.clear();
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.canvas-board {
  width: 100%;
  height: 100%;
  position: relative;
  background-color: #fff;

  .canvas-container {
    width: 100%;
    height: 100%;
    overflow: auto;
    position: relative;

    #canvas {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      background-color: #fff;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    }
  }
}
</style> 